﻿
namespace BlogEngine.Setup
{
    static class Program
    {
        static void Main(string[] args) // TODO: Move all the account settings to App.config
        {
            var local = args.Length == 0;
            if (!local)
                if (string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]))
                    throw new System.ArgumentException("To run on Azure, pass the account & accessKey");

            var address = local ? "http://127.0.0.1:10002" : "https://table.core.windows.net";
            var account = local ? "devstoreaccount1" : args[0];
            var accessKey = local
                ? "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
                : args[1];

            const string database = "BlogEngine"; // If you want test with Development Storage, change to database choosed for your DevStorage
            const string appName = "BlogEngine";

            var ai = new Microsoft.Samples.ServiceHosting.StorageClient.StorageAccountInfo(
                new System.Uri(address), null, account, accessKey);

            if (local)
            {
                System.Console.WriteLine("Local storage table creation");
                System.Console.WriteLine("Recreate database '" + database + "' (press the key 'Y') ?");
                System.Console.WriteLine("(Make sure that it isn't currently used by the Development Storage)");
                var key = System.Console.ReadKey(true);
                if (key.Key == System.ConsoleKey.Y)
                {
                    var contextPath = typeof(Core.BlogSettings).Assembly.Location;
                    var proc = new System.Diagnostics.Process();
                    if (database.Length > 0)
                        proc.StartInfo.Arguments = "/database:" + database;
                    proc.StartInfo.Arguments += " /forceCreate " + contextPath;
                    proc.StartInfo.FileName = System.IO.Path.Combine(
                        System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles),
                        @"Windows Azure SDK\v1.0\bin\DevtableGen.exe");
                    proc.StartInfo.CreateNoWindow = false;
                    // proc.StartInfo.RedirectStandardOutput = true;
                    // proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.UseShellExecute = false;

                    System.Console.WriteLine("Starting DevtableGen " + proc.StartInfo.Arguments);
                    proc.Start();
                    proc.WaitForExit();
                    System.Console.WriteLine();
                    System.Console.WriteLine("Start DevServer, make sure that the database '" + database + "' is selected, and press a key...");
                    System.Console.ReadKey();
                    System.Console.WriteLine();
                }
            }
            else
            {
                Core.Azure.BlogProvider.Setup.CreateTables(ai);
            }

            Core.Azure.BlogProvider.Setup.FillTables(ai, appName);

            System.Console.WriteLine("Done! Press a key...");
            System.Console.ReadKey();
        }
    }
}
